home *** CD-ROM | disk | FTP | other *** search
/ Network CD 1 / Network CD.iso / others / parnfs / hancrtso.a < prev    next >
Text File  |  1993-12-02  |  7KB  |  286 lines

  1. ; $Id: hancrtso.a,v 1.1 1993/12/02 20:54:04 Rhialto Exp $
  2. ; $Log: hancrtso.a,v $
  3. ; Revision 1.1  1993/12/02  20:54:04  Rhialto
  4. ; Initial revision
  5. ;
  6. resident    equ 1    ; Do we handle RESIDENT (== pure) code?
  7. ;        near a4 ; for A68k 2.7.1: also needs -m32766 for offset
  8.         ;   C.A
  9.         ;
  10.         ;   (c)Copyright 1990, Matthew Dillon, All Rights Reserved
  11.         ;
  12.         ;   Amiga startup code for non-resident and resident DCC
  13.         ;   programs.
  14.         ;
  15.         ;   Modified for use with a DOS handler.
  16.         ;
  17.         ;   DLINK:
  18.         ;
  19.         ;   * __BSS_LEN   : # long wrds of bss (occurs after DATA)
  20.         ;   * __DATA_LEN  : # long wrds of initialized data to copy (if resident)
  21.         ;   * __DATA_BAS  : base of data (read only if resident)
  22.         ;   * __RESIDENT  : is this code residentable?    If this variable
  23.         ;            is set then there are no absolute data/bss
  24.         ;            references except __DATA_BAS and there is
  25.         ;            NO BSS MEMORY ALLOCATED AFTER THE DATA
  26.         ;            SECTION.
  27.         ;
  28.         ;            If this variable is not set then absolute
  29.         ;            references refer to the same storage as
  30.         ;            A4 relative accesses and uninitialized
  31.         ;            BSS space exists just after the data section.
  32.         ;
  33.         ;            Note that in the large-data model + fragmented
  34.         ;            sections, there are usually real BSS hunks
  35.         ;            floating about that have been zerod by
  36.         ;            LoadSeg() (in this case __BSS_LEN could be 0)
  37.         ;
  38.         ;   COMPILER:
  39.         ;
  40.         ;   * If -r option specified when compiling then the compiler
  41.         ;     will generate autoinit code to handle data initialization
  42.         ;     that would otherwise require a reloc32 hunk.
  43.         ;
  44.         ;   * If -r or -md specified compiler references all data items
  45.         ;     using A4-Relative addressing.
  46.         ;
  47.         ;   C LIB CODE:
  48.         ;
  49.         ;   * Resident: Allocate (__DATA_LEN+__BSS_LEN)*4
  50.         ;     Not Res : zero out preallocated BSS.
  51.         ;
  52.         ;   * Generate A4 ref ptr to bss base + 32766 (uses Aztec's
  53.         ;     conventions).
  54.         ;
  55.         ;   * All library code uses the SMALL-DATA with alternate
  56.         ;     BSS and DATA section names (based in this source
  57.         ;     file) so the linker puts library related data/bss
  58.         ;     first.  BUT, BSS always comes after DATA unless
  59.         ;     you -frag the link (can't resident a frag'd link),
  60.         ;     so if there is more than 64KBytes of *initialized*
  61.         ;     data you must -frag the link.
  62.  
  63.         section text,code
  64.  
  65.         xref    __RESIDENT        ; (dlink), executable is residentable
  66.         xref    __BSS_LEN        ; (dlink), length of BSS
  67.         xref    __DATA_BAS        ; (dlink), base of initialized data
  68.         xref    __DATA_LEN        ; (dlink), length of data
  69.  
  70.         xref    _handlermain        ; we call handlermain()
  71.  
  72.         xref    _LVOSetSignal
  73.         xref    _LVOAllocMem
  74.         xref    _LVOFreeMem
  75.         xref    _LVOForbid
  76.         xref    _LVOReplyMsg
  77.  
  78.         xdef    _exit            ; we are exit()
  79.         xdef    start
  80.         xdef    _SysBase        ; we export _SysBase
  81. ;        xdef    __WBMsg
  82.  
  83. MEMF_CLEAR    equ    $00000001
  84. MEMF_PUBLIC    equ    $00010000
  85.  
  86. start:
  87. ;        movem.l D2-D7/A2-A6,-(sp)
  88.  
  89.         move.l    4,A6            ; EXEC base
  90.  
  91.         move.l    sp,A2
  92. ;        move.l    A0,-(sp)            ; save arg for _main() call
  93. ;        move.l    D0,-(sp)            ; save arglen for _main() call
  94.  
  95.         ;   If we are flagged resident then there is NO BSS ALLOCATED
  96.         ;   If we are not flagged resident then BSS IS ALLOCATED AFTER DATA,
  97.         ;    BUT NOT CLEARED.
  98.  
  99.         ;ifne resident
  100.         move.w    #__RESIDENT,D0
  101.         beq    snotres
  102.  
  103.         ;   Allocate BSS+DATA space and then copy static data.
  104.  
  105.         move.l    #__BSS_LEN,D0
  106.         add.l    #__DATA_LEN,D0
  107.         asl.l    #2,D0            ; x4
  108.         addq.l    #8,D0            ; MemList header extra
  109.         move.l    D0,D5            ; D5 = #bytes
  110.         move.l    #MEMF_PUBLIC,D1
  111.         jsr    _LVOAllocMem(A6)
  112.         tst.l    D0
  113.         bne    alok
  114.         move.l    A2,sp
  115.         moveq.l #-1,D0
  116.         bra    exfail
  117.  
  118. alok        move.l    D0,A0
  119.         clr.l    (A0)+               ; MemList entry next ptr
  120.         move.l    D5,(A0)+            ; MemList entry #bytes
  121.         lea    32766(A0),A4        ; SET A4
  122.         lea    -8(A0),A3           ; A3 = MemList entry base
  123.                         ; can't copy to MemList(A4) yet
  124.  
  125.  
  126.         ;   Copy data to allocated copy
  127.  
  128.                         ; A0 = dst
  129.         lea    __DATA_BAS,A1        ; A1 = src
  130.         move.l    #__DATA_LEN,D0        ; D0 = long words
  131.         bra    bssent
  132. bsslop        move.l    (A1)+,(A0)+
  133. bssent        dbf    D0,bsslop
  134.         sub.l    #$10000,D0
  135.         bcc    bsslop
  136.         bra    clrbss
  137.         ;endc
  138.  
  139.  
  140.         ;   Not resident, BSS space has been allocated for us
  141.         ;   beyond the specified data, just load the base ptr
  142.  
  143. snotres     lea    __DATA_BAS,A4
  144.         lea    32766(A4),A4
  145.         sub.l    A3,A3
  146.  
  147. clrbss
  148.         ;   CLEAR BSS    &-32766(A4) + __DATA_LEN*4
  149.  
  150.         lea    -32766(A4),A0
  151.         move.l    #__DATA_LEN,D0
  152.         asl.l    #2,D0
  153.         add.l    D0,A0
  154.  
  155.         move.l    #__BSS_LEN,D0        ; longwords of bss
  156.         moveq.l #0,D1
  157.         bra    clrent
  158. clrlop        move.l    D1,(A0)+
  159. clrent        dbf    D0,clrlop
  160.         sub.l    #$10000,D0
  161.         bcc    clrlop
  162.  
  163.         ;ifne resident
  164.         move.l    A3,___MemList(A4)   ; memlist entry (if resident)
  165.         ;endc
  166.         move.l    A2,__ExitSP(A4)     ; sp to restore
  167.  
  168. ;        moveq.l #0,D0            ; new signals
  169. ;        move.l    #$1000,D1        ; signal mask
  170. ;        jsr    _LVOSetSignal(A6)   ; clear ^C
  171.  
  172.         move.l    A6,_SysBase(A4)     ; resident segment.
  173.  
  174.         ;   Return value of 0 indicates succcess.  Flags
  175.         ;   have been set.
  176.  
  177.         ;ifne resident
  178.         jsr    __AutoInit0        ; A6 has SYSBase
  179.         bne    xfail
  180. ;        pea    1.W            ; autoconfig loader
  181. ;        jsr    __AutoConfig        ; note, rt pulls stack
  182. ;        addq.l    #4,sp
  183.         jsr    __AutoInit1        ; A6 has SYSBase
  184.         bne    xfail
  185.         ;endc
  186.         jsr    _handlermain(PC)
  187.  
  188.         ;   fall through to low level exit... this avoids referencing
  189.         ;   exit() if the user overides handlermain().
  190.  
  191. xfail
  192.         clr.l    -(sp)
  193.         clr.l    -(sp)
  194.  
  195.         ;   _EXIT()
  196.         ;
  197.         ;   since entry uses malloc we must free any incidental memory
  198.         ;   at __exit instead of _exit.
  199.         ;
  200.         ;   ReplyMsg(_WBMsg) just before returning
  201.  
  202. _exit:
  203. ;        pea    -1.W
  204. ;        jsr    __AutoConfig        ; note, rt pulls stack
  205. ;        addq.l    #4,sp
  206.         move.l    _SysBase(A4),A6
  207.         ;ifne resident
  208.         jsr    __AutoExit1        ; A6 has SysBase
  209.         jsr    __AutoExit0        ; A6 has SysBase
  210.         ;endc
  211.  
  212.         move.l    __ExitSP(A4),A5     ; get sp... because we might free
  213.                         ; the space taken by the variable!
  214.  
  215. ;        move.l    __WBMsg(A4),D6      ; D6 = WBMsg if it exists
  216.  
  217.         ;ifne resident
  218.         move.l    ___MemList(A4),D0   ; free memory
  219.         beq    ex20
  220. ex10        move.l    D0,A2
  221.         move.l    (A2),A3             ; next...
  222.  
  223.         move.l    4(A2),D0            ; bytes
  224.         move.l    A2,A1            ; ptr
  225.         jsr    _LVOFreeMem(A6)
  226.  
  227.         move.l    A3,D0            ; next...
  228.         bne    ex10
  229.         ;endc
  230. ex20
  231.  
  232.         move.l    4(sp),D0            ; get exit code
  233.         move.l    A5,sp            ; restore sp
  234.  
  235. ;        tst.l    D6            ; reply to workbench msg if it
  236. ;        beq    ex30            ; exists
  237. ;        jsr    _LVOForbid(A6)      ; forbid through exit
  238. ;        move.l    D6,A1
  239. ;        jsr    _LVOReplyMsg(A6)
  240.  
  241. ex30
  242.         ;   FINIS, poof.
  243.  
  244. exfail
  245. ;        movem.l (sp)+,D2-D7/A2-A6
  246.         rts
  247.  
  248.  
  249.         ;   Base of autoinit section
  250.  
  251.         ;ifne resident
  252.         section autoinit0,code
  253. __AutoInit0:
  254.         section autoinit1,code
  255. __AutoInit1:
  256.         section autoexit0,code
  257. __AutoExit0:
  258.         section autoexit1,code
  259. __AutoExit1:
  260.         ;endc
  261. ;        section autoconfig,code
  262. ;__AutoConfig:
  263.  
  264.  
  265.         ;   All library C code is compiled with the -S option
  266.         ;   which uses 'libdata' and 'libbss' section names,
  267.         ;   forcing library data to come before program data
  268.         ;   and library bss to come before program bss (because
  269.         ;   library data/bss sections are declared here first
  270.         ;   and sections of like name are coagulated).
  271.  
  272.         section libdata,data
  273. _Reserved    dc.l    0        ; force section to exist (dummy)
  274.  
  275.         section libbss,bss
  276.  
  277. _SysBase    ds.l    1
  278. __ExitSP    ds.l    1
  279. ;__WBMsg    ds.l    1
  280.         ;ifne resident
  281.         xdef    ___MemList    ; used by malloc/free
  282. ___MemList    ds.l    1
  283.         ;endc
  284.  
  285.     END
  286.